//
//  Movie.swift
//  MovieAppDemo
//
//  Created by Demo on 2025/1/25.
//
import Foundation
 
struct ComicPicsResponse: Decodable {
    var code: Int
    var msg: String?
    var data: [ComicPics]
    var total: Int
    var page: Int
    var page_size: Int
}



struct ComicPics: Identifiable, Decodable, Equatable {
    var id: Int
    var url: String
    var cover: String
    
    enum CodingKeys: String, CodingKey {
        case id, url, cover
    }
    
    static func ==(lhs: ComicPics, rhs: ComicPics) -> Bool {
            return lhs.id == rhs.id && lhs.cover == rhs.cover
        }
}




var comicpics: [ComicPics] = [
    ComicPics(
        id: 1,
        url: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/27.jpg",
        cover: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/27.jpg"
    ),
    ComicPics(
        id: 2,
        url: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/28.jpg",
        cover: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/28.jpg"
    ),
    ComicPics(
        id: 3,
        url: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/29.jpg",
        cover: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/29.jpg"
    ),
    ComicPics(
        id: 4,
        url: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/30.jpg",
        cover: "http://www.cosck.com:444/usb/comic/pics/30/5821859d5f6b9a4f93dbf719/5821859e5f6b9a4f93dbf71b/30.jpg"
    )
]





class ComicPicsViewModel: ObservableObject {
    @Published var isLoading = true
    @Published var errorMessage: String?
    @Published var pics: [ComicPics] = []
    @Published var count = 0
    

    func fetchComicPics(ep_id: Int, completion: @escaping ([ComicPics]?) -> Void) {
        guard let url = URL(string: "http://www.cosck.com:39999/api/pica/pica_main/get_pics?ep_id=\(ep_id)") else {
            self.errorMessage = "Invalid URL"
            completion(nil)
            return
        }
        
        var request = URLRequest(url: url)
        request.httpMethod = "GET"  // 设置请求方法
        request.setValue("dev", forHTTPHeaderField: "token")  // 设置 header: token=dev
        
        isLoading = true
        URLSession.shared.dataTask(with: request) { data, response, error in
            DispatchQueue.main.async {
                self.isLoading = false
                
                if let error = error {
                    self.errorMessage = "Error: \(error.localizedDescription)"
                    completion(nil)
                    return
                }
                
                guard let data = data else {
                    self.errorMessage = "No data received"
                    completion(nil)
                    return
                }
                
                do {
                    // 解码为 Response 类型
                    let decodedResponse = try JSONDecoder().decode(ComicPicsResponse.self, from: data)
                    completion(decodedResponse.data)  // 将 data 数组传递给 completion handler
                } catch {
                    self.errorMessage = "Failed to decode JSON: \(error.localizedDescription)"
                    print("Failed to decode JSON: \(error)")
                    completion(nil)
                }
            }
        }.resume()
    }
    func get_list(ep_id: Int, completion: @escaping (Int) -> Void) {
        fetchComicPics(ep_id: ep_id) { data in
            DispatchQueue.main.async {
                if let data = data {
                    self.pics = data
                    self.count = self.pics.count
                    self.isLoading = false
                    completion(self.count) // 调用回调返回 count
                }
            }
        }
    }



}




